package com.dm.wallpaper.library.ablility.slice;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.adapters.FilterProvider;
import com.dm.wallpaper.library.helpers.TimeHelper;
import com.dm.wallpaper.library.preferences.Preferences;
import com.dm.wallpaper.library.view.MiCommonDialog;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.LayoutAlignment;
/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
public class WallpaperBoardMuzeiMainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    Text select_time_info;
    boolean isMinute = true;
    boolean mIsMuzei = true;
    int minuteValue = 30;
    MiCommonDialog commonDialog;
    MiCommonDialog categriesdialog;
    Checkbox only_wifi_checkbox;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_wallpaper_board_muzei_main);
        initView();
        initDate();
    }

    private void initView() {
        findComponentById(ResourceTable.Id_select_time).setClickedListener(this);
        select_time_info = (Text) findComponentById(ResourceTable.Id_select_time_info);
        only_wifi_checkbox = (Checkbox) findComponentById(ResourceTable.Id_only_wifi_checkbox);
        findComponentById(ResourceTable.Id_only_wifi_layout).setClickedListener(this);
        findComponentById(ResourceTable.Id_select_categries).setClickedListener(this);
        findComponentById(ResourceTable.Id_save_muzei_setting).setClickedListener(this);

    }

    private void initDate() {
        minuteValue = Preferences.get().getRotateTime();
        isMinute = Preferences.get().isRotateMinute();
        select_time_info.setText("Evrty " + minuteValue + " " + (isMinute ? "Minute" : "Hour"));
        only_wifi_checkbox.setChecked(Preferences.get().isWifiOnly());
    }



    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_select_time) {
            showSelectTimeDialog();
        } else if (component.getId() == ResourceTable.Id_close) {
            commonDialog.destroy();
        } else if (component.getId() == ResourceTable.Id_only_wifi_layout) {
            only_wifi_checkbox.setChecked(!only_wifi_checkbox.isChecked());
        } else if (component.getId() == ResourceTable.Id_select_categries) {
            showSelectCategriesDialog();
        } else if (component.getId() == ResourceTable.Id_save_muzei_setting) {
            int rotateTime = TimeHelper.minuteToMilli(minuteValue);
            if (!isMinute) rotateTime = rotateTime * 60;
            Preferences.get().setRotateMinute(isMinute);
            Preferences.get().setRotateTime(rotateTime);
            Preferences.get().setWifiOnly(only_wifi_checkbox.isChecked());


//            Log.i("WallpaperBoardMuzeiMainAbilitySlice.onClick([component]):80");
//            Intent intent = new Intent();
//            ElementName localElement = new ElementName("", getBundleName(), "WallpaperServiceAbility");
//            intent.setElement(localElement);
////            connectAbility(intent, connection);
//            Timer.TimerIntent timerIntent = new Timer.TimerIntent(intent, Timer.ABILITY_TYPE_SERVICE);
////            Timer.RepeatTimer timer = Timer.RepeatTimer.getTimer(WallpaperBoardApplication.getInstance(), timerIntent);
////            timer.start(Timer.TIMER_TYPE_WAKEUP, 5000, 5000);
//            Timer.OneShotTimer oneShotTimer = Timer.OneShotTimer.getTimer(WallpaperBoardApplication.getInstance(), timerIntent);
//            oneShotTimer.start(Timer.TIMER_TYPE_WAKEUP, 5000);

        }
    }

//    private IAbilityConnection connection = new IAbilityConnection() {
//
//        @Override
//
//        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
//            Log.i("WallpaperBoardMuzeiMainAbilitySlice.onAbilityConnectDone([elementName, iRemoteObject, resultCode]):98");
//        }
//
//        @Override
//
//        public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
//            Log.i("WallpaperBoardMuzeiMainAbilitySlice.onAbilityDisconnectDone([elementName, resultCode]):104");
//        }
//
//    };

    private void showSelectCategriesDialog() {
        categriesdialog = new MiCommonDialog(WallpaperBoardMuzeiMainAbilitySlice.this);
        Component content = LayoutScatter.getInstance(WallpaperBoardMuzeiMainAbilitySlice.this).parse(ResourceTable.Layout_dailog_select_categries_layout, null, false);

        Text categries_title = (Text) content.findComponentById(ResourceTable.Id_categries_title);
        categries_title.setText(mIsMuzei ? ResourceTable.String_muzei_category : ResourceTable.String_wallpaper_filter);


        ListContainer listContainer = (ListContainer) content.findComponentById(ResourceTable.Id_catergies_list);
        listContainer.setItemProvider(new FilterProvider(WallpaperBoardMuzeiMainAbilitySlice.this, mIsMuzei));

        Checkbox select_all = (Checkbox) content.findComponentById(ResourceTable.Id_select_all);
        select_all.setChecked(((FilterProvider) listContainer.getItemProvider()).checkAllSelect());
        select_all.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                ((FilterProvider) listContainer.getItemProvider()).selectAllorNot();
                if (b) {
                    VectorElement vectorElement = new VectorElement(WallpaperBoardMuzeiMainAbilitySlice.this, ResourceTable.Graphic_svg_toolbar_select_all_selected);
                    select_all.setButtonElement(vectorElement);
                } else {
                    VectorElement vectorElement = new VectorElement(WallpaperBoardMuzeiMainAbilitySlice.this, ResourceTable.Graphic_svg_toolbar_select_all);
                    select_all.setButtonElement(vectorElement);
                }
            }
        });

        categriesdialog.setAutoClosable(true);
        categriesdialog.setContentCustomComponent(content).setAlignment(LayoutAlignment.CENTER).setTransparent(true);
        categriesdialog.addDialogListener(new MiCommonDialog.CommonDialogListener() {
            @Override
            public void hide() {
            }

            @Override
            public void destroy() {

            }
        });
        categriesdialog.show();
    }


    private void showSelectTimeDialog() {
        commonDialog = new MiCommonDialog(WallpaperBoardMuzeiMainAbilitySlice.this);
        Component content = LayoutScatter.getInstance(WallpaperBoardMuzeiMainAbilitySlice.this).parse(ResourceTable.Layout_dailog_select_time_layout, null, false);
        RadioButton minute_button = (RadioButton) content.findComponentById(ResourceTable.Id_minute_button);
        RadioButton hour_button = (RadioButton) content.findComponentById(ResourceTable.Id_hour_button);
        Text close = (Text) content.findComponentById(ResourceTable.Id_close);
        minute_button.setClickedListener(this);
        hour_button.setClickedListener(this);
        close.setClickedListener(this);
        Picker number_picker = (Picker) content.findComponentById(ResourceTable.Id_number_picker);
        number_picker.setValue(minuteValue);
        if (isMinute) {
            minute_button.setChecked(true);
        } else {
            hour_button.setChecked(true);
        }
        commonDialog.setAutoClosable(true);
        commonDialog.setContentCustomComponent(content).setAlignment(LayoutAlignment.CENTER).setTransparent(true);
        commonDialog.addDialogListener(new MiCommonDialog.CommonDialogListener() {
            @Override
            public void hide() {
            }

            @Override
            public void destroy() {
                Preferences.get().setRotateTime(number_picker.getValue());
                Preferences.get().setRotateMinute(minute_button.isChecked());
                initDate();
            }
        });
        commonDialog.show();
    }
}
